from core.apiclient import ApiClient
from utils.assertutil import AssertUtil
from utils.extractutil import ExtractUtil


class ApiServer:
    def __init__(self):
        self.session = ApiClient()
        self.assertutil = AssertUtil()
        self.extrac1 = ExtractUtil()

    def info(self, data, login_data=None):
        # 获取requests_info
        requests_info = data["requests_info"]
        # 获取url
        url = self.extrac1.new_url(requests_info["url"])
        # url = requests_info["url"]
        # 获取method
        method = requests_info["method"]
        # 获取headers
        headers = requests_info["headers"]
        if login_data:
            headers.update(login_data)
        # 获取case_info
        case_info = data["case_info"]

        # 获取 validate
        validate = case_info.pop("validate", None)

        # 获取extract
        extract = case_info.pop("extract", None)

        responds_json = self.session.do_request(url=url, method=method, headers=headers, **case_info)

        self.extrac1.extractUtil(responds_json, extract)
        # 断言处理
        self.assertutil.assert_servel(responds_json, validate)
